#include <AudioMessager.h>
#include <unistd.h>

#ifdef __ANDROID__
#include <android/log.h>
#define BOSE_LOGI(...) __android_log_print(ANDROID_LOG_INFO,  "BOSECMD_AM", __VA_ARGS__)
#define BOSE_LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, "BOSECMD_AM", __VA_ARGS__)
#define BOSE_LOGW(...) __android_log_print(ANDROID_LOG_WARN,  "BOSECMD_AM", __VA_ARGS__)
#define BOSE_LOGE(...) __android_log_print(ANDROID_LOG_ERROR, "BOSECMD_AM", __VA_ARGS__)
#else
#define BOSE_LOGI(str, ...) do{printf("BOSECMD_AM ");printf(str,##__VA_ARGS__);printf("\n");}while(0)
#define BOSE_LOGD(str, ...) do{printf("BOSECMD_AM ");printf(str,##__VA_ARGS__);printf("\n");}while(0)
#define BOSE_LOGW(str, ...) do{printf("BOSECMD_AM ");printf(str,##__VA_ARGS__);printf("\n");}while(0)
#define BOSE_LOGE(str, ...) do{printf("BOSECMD_AM ");printf(str,##__VA_ARGS__);printf("\n");}while(0)
#endif

using namespace com::bose;

AudioMessager::~AudioMessager() {
    BOSE_LOGD("AudioMessager Destructor");
    if(mPlayer != nullptr) {
        delete mPlayer;
        mPlayer = nullptr;
    }
}

AudioMessager::AudioMessager() {
    BOSE_LOGD("AudioMessager Constructor");
}

session_id AudioMessager::play_wav(const std::string url) {
    if(mPlayer == nullptr) {
        mPlayer = new AudioPlayer(url);
    }

    if (get_player_status() == PLAY_STATUS_PLAYING) {
        BOSE_LOGE("Previous instance is still playing!!");
        return AP_SESSION_ID_INVALID;
    }
    return mPlayer->play();
}

ErrorCode AudioMessager::stop_wav(session_id id) {
    if(mPlayer != nullptr) {
        ErrorCode ret = mPlayer->stop(id);
        return ret;
    }
    return AP_ERRORCODE_INVALID_STATUS;
}

PlayStatus AudioMessager::get_player_status() {
    return mPlayer->ap_get_status();
}

ErrorCode AudioMessager::pause_wav(session_id id, bool pause) {
    if(mPlayer != nullptr) {
        return mPlayer->pause(id, pause);
    }
    return AP_ERRORCODE_INVALID_STATUS;
}

ErrorCode AudioMessager::seek_to(int32_t seconds){
    if(mPlayer != nullptr) {
        return mPlayer->seek(seconds);
    }
    return AP_ERRORCODE_INVALID_STATUS;
}